package com.zh.api.controller.equipment;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.equipment.domain.CardDO;
import com.zh.equipment.service.EquipmentCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-04-24 15:20:55
 */
 
@Controller
@RequestMapping("/equipment/card")
@Api("用户开卡")
public class CardController extends BaseController {
	@Autowired
	private EquipmentCardService cardService;

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取用户开卡列表")
	public ResultVO list(@RequestBody Map<String, Object> params){

		if (CheckEmptyUtil.isEmpty(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		//查询列表数据
        Query query = new Query(params);
		List<CardDO> cardList = cardService.list(query);
		int total = cardService.count(query);
		PageUtils pageUtils = new PageUtils(cardList, total);
		return ResultVO.success(pageUtils);
	}


	@RequestMapping(value = "getCardById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取开卡用户信息")
	@ResponseBody
	ResultVO getCardById(@RequestBody CardDO cardDO){
		if(cardDO==null||CheckEmptyUtil.isEmpty(cardDO.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		CardDO card = cardService.get(cardDO.getId());
	    return ResultVO.success(card);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("保存用户开卡信息")
	public ResultVO save(@RequestBody CardDO card){
		if(card==null||CheckEmptyUtil.isEmptys(card.getCardNumber()
				,card.getBalance(),card.getFamilyUserId()
					,card.getType(),card.getState(),card.getExpiretime())){

			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(cardService.save(card)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("修改用户开卡信息")
	public ResultVO update(@RequestBody CardDO card){

		if(card==null||CheckEmptyUtil.isEmptys(card.getId()
				,card.getBalance(),card.getFamilyUserId(),card.getFamilyUserName()
				,card.getType(),card.getState(),card.getExpiretime())){

			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		if(cardService.update(card)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除用户开卡信息")
	public ResultVO remove(@RequestBody CardDO card){

		if(card==null||CheckEmptyUtil.isEmpty(card.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		if(cardService.remove(card.getId())>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除用户开卡信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		List<String> ids= (List<String>) jsonParam.get("ids");

		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		String[] strs1=ids.toArray(new String[ids.size()]);
		if(cardService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}


	
}
